#include "atom.h"
#include "wlan.h"
#include <string.h>
#include <stdio.h>
#include "syslog.h"


/* Create WLAN device */
void WlanCreate(Wlan *dev, const char *name, const WlanOps *ops) 
{
    /* Initialize base device */
    IosDrvInstall(&dev->super, name);
    
    /* Initialize WLAN specific fields */
    dev->Protected.mode = WLAN_MODE_NONE;
    dev->Protected.ops = ops;
}

/* WLAN device operations */
int WlanSetMode(Wlan *dev, WlanMode_t mode) 
{
    if (dev && dev->Protected.ops && dev->Protected.ops->set_mode){
        return dev->Protected.ops->set_mode(dev, mode);
    }
    return NG;
}

WlanMode_t WlanGetMode(Wlan *dev)
{
    if (dev && dev->Protected.ops && dev->Protected.ops->get_mode){
        return dev->Protected.ops->get_mode(dev);
    }
    return WLAN_MODE_NONE;
}

int WlanScan(Wlan *dev, WlanScanInfo *info, int max_results)
{
    if (dev && dev->Protected.ops && dev->Protected.ops->scan) {
        return dev->Protected.ops->scan(dev, info, max_results);
    }
    return NG;
}

int WlanConnect(Wlan *dev, const char *ssid, const char *password, uint32_t timeout_sec) 
{
    int ret = NG;
    
    if (dev && dev->Protected.ops && dev->Protected.ops->connect) {
        ret = dev->Protected.ops->connect(dev, ssid, password, timeout_sec);
        
        // 如果连接成功且有保存配置功能，保存当前配置
        if (ret == OK && dev->Protected.ops->save_config) {
            dev->Protected.ops->save_config(dev, ssid, password);
        }
    }
    return ret;
}

int WlanStartAP(Wlan *dev, const char *ssid, const char *password)
{
    if (dev && dev->Protected.ops && dev->Protected.ops->startap) {
        return dev->Protected.ops->startap(dev, ssid, password);
    }
    return NG;
}

int WlanDisconnect(Wlan *dev)
{
    if (dev && dev->Protected.ops && dev->Protected.ops->disconnect) {
        return dev->Protected.ops->disconnect(dev);
    }
    return NG;
}

int WlanGetNetInfo(Wlan *dev, WlanNetInfo *info)
{
    if (dev && dev->Protected.ops && dev->Protected.ops->get_net_info) {
        return dev->Protected.ops->get_net_info(dev, info);
    }
    return NG;
}

int WlanSmartConfig(Wlan *dev, const char *name, uint32_t timeout_sec) {
    int ret;
    char ssid[33] = {0};  // SSID最大32字节
    char password[65] = {0};  // 密码最大64字节
    uint8_t mac[6];
    
    SYSLOG(LOG_INFO, "Starting WlanSmartConfig...");
    
    // 从E2P读取已保存的SSID和密码
    if (dev->Protected.ops->read_config) {
        SYSLOG(LOG_INFO, "Reading saved WiFi config from E2P");
        ret = dev->Protected.ops->read_config(dev, ssid, password);
        
        // 如果有保存的配置，先尝试连接
        if (ret == OK && strlen(ssid) > 0) {
            SYSLOG(LOG_INFO, "Found saved config, SSID: %s", ssid);
            
            // 设置为station模式
            SYSLOG(LOG_INFO, "Setting WLAN mode to STATION");
            WlanSetMode(dev, WLAN_MODE_STATION);
            
            // 尝试连接已保存的网络
            SYSLOG(LOG_INFO, "Trying to connect to saved network...");
            ret = WlanConnect(dev, ssid, password, timeout_sec);
            if (ret == OK) {
                SYSLOG(LOG_INFO, "Successfully connected to saved network");
                return OK;  // 连接成功
            }
            SYSLOG(LOG_WARNING, "Failed to connect to saved network");
        } else {
            SYSLOG(LOG_INFO, "No saved WiFi config found");
        }
    }
    
    SYSLOG(LOG_INFO, "Starting AP mode configuration");
    
    // 如果没有保存的配置或连接失败，启动AP模式
    // 获取MAC地址
    if (dev->Protected.ops->get_mac) {
        SYSLOG(LOG_INFO, "Getting MAC address");
        dev->Protected.ops->get_mac(dev, mac);
    } else {
        SYSLOG(LOG_WARNING, "Failed to get MAC address, using default");
        // 如果无法获取MAC地址，使用默认值
        memset(mac, 0, sizeof(mac));
    }
    
    // 构造AP的SSID: name + MAC后6位
    snprintf(ssid, sizeof(ssid), "%s_%02X%02X%02X", 
             name, mac[3], mac[4], mac[5]);
    SYSLOG(LOG_INFO, "Generated AP SSID: %s", ssid);
    
    // 设置为AP模式
    SYSLOG(LOG_INFO, "Setting WLAN mode to AP");
    WlanSetMode(dev, WLAN_MODE_AP);
    
    // 启动AP，使用固定密码"1234567890"
    SYSLOG(LOG_INFO, "Starting AP...");
    ret = WlanStartAP(dev, ssid, "1234567890");
    
    if (ret == OK) {
        SYSLOG(LOG_INFO, "AP started successfully");
    } else {
        SYSLOG(LOG_ERR, "Failed to start AP");
    }
    
    return ret;
}
